/**
 * @function: 页面抓取工具类
 * @modify history
 * Date           Author            Description
 * --------------------------------------------------------
 * 2010/10/25    yuanligang        Created
 *  
 */
package com.byxm.wlgw.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebContent {
	
	/**
	 * 读取一个网页全部内容
	 * @param htmlUrl 网址
	 * @param encode 网站的编码方式
	 */
	public final static String getOneHtml(String htmlurl,String encode) throws IOException {
		URL url;
		String html = "";
		String temp;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(htmlurl);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream(), encode));// 读取网页全部内容
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
			//对html内容进行处理  将多个空格替换为一个空格
			String regEx="[' ']+";   //一个或多个空格   
	        Pattern p = Pattern.compile(regEx);   
	        Matcher m = p.matcher(sb.toString()); 
	        html = m.replaceAll(" ");
		} catch (final MalformedURLException me) {
			System.out.println("你输入的URL格式有问题！请仔细输入");
			me.getMessage();
			throw me;
		} catch (final IOException e) {
			e.printStackTrace();
			throw e;
		}
		return html;
	}

	/**
	 * 
	 * @param s
	 * @return 获得网页标题
	 */
	public  final static  String getTitle(String s) {
		String regex;
		String title = "";
		List<String> list = new ArrayList<String>();
		regex = "<title>.*?</title>";
		Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
		Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		for (int i = 0; i < list.size(); i++) {
			title = title + list.get(i);
		}
		return outTag(title);
	}

	/**
	 * 
	 * @param s
	 * @return 获得链接
	 */
	public  final static String getLink(String s) {
		String regex;
		String url = "";
		regex = "<a[\\s]+href[\\s]*=[\\s]*\"([^<\"]+)\"";
		Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
		Matcher ma = pa.matcher(s);
		while (ma.find()) {
			url = ma.group();
			url = url.substring(url.indexOf("=")+2,url.lastIndexOf('"'));
		}
		return url;
	}

	/**
	 * 
	 * @param s
	 * @return 获得脚本代码
	 */
	public  final static  List<String> getScript(String s) {
		String regex;
		List<String> list = new ArrayList<String>();
		regex = "<script.*?</script>";
		Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
		Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}

	/**
	 * 
	 * @param s
	 * @return 获得CSS
	 */
	public  final static  List<String> getCSS(String s) {
		String regex;
	    List<String> list = new ArrayList<String>();
		regex = "<style.*?</style>";
		Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
		Matcher ma = pa.matcher(s);
		while (ma.find()) {
			list.add(ma.group());
		}
		return list;
	}

	/**
	 * 
	 * @param s
	 * @return 去掉标记
	 */
	public  final static String outTag(String s) {
		//去掉标记
		s = s.replaceAll("<.*?>", "");
		//去除nbsq空格
		s = s.trim();
		s = s.replaceAll(" ", "");
		s = s.replaceAll("&nbsp;","");
		s = s.replaceAll("&nbsp","");
		return s;
	}

	
	
	/**
	 * 获取页面中匹配的内容
	 * @param url页面的链接地址
	 * @param regex 所要获取内容的正则表式
	 * @param encode 网站的编码方式
	 * @return 
	 */
	public final static String[] getContent(String url,String regex,String encode) {	
		Matcher ma = null;
		String html = "";
		String[] content = new String[7];
		try {
			//获取页面内容
			html = getOneHtml(url,encode);
			//System.out.println(html);
			Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
			ma = pa.matcher(html);
			int i = 0;
			while(ma.find() && i < 7){
				content[i] = ma.group();
				//System.out.println(ma.group());
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return content;
	}
	
	
	
	
}